package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.crm.model.Attachment;
import com.crm.mapper.AttachmentMapper;
import com.crm.model.vo.AttachmentVO;
import com.crm.model.vo.CompanyVO;
import com.crm.service.IAttachmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 * @author zjd
 * @since 2021-04-01
 */
@Service
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment> implements IAttachmentService {



    @Override
    public void deleteByindexIdAndType(Long indexId, Integer type) {
        QueryWrapper<Attachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Attachment::getIndexId, indexId)
                .eq(Attachment::getType,type);
        List<Attachment> list = list(wrapper);
        if (list !=null && list.size()>0){
           // baseMapper.delete(wrapper);
            for (Attachment attachment : list) {
                attachment.setType(0);
                attachment.setIndexId(0L);
                baseMapper.updateById(attachment);
            }
        }

    }

    @Override
    public List<AttachmentVO> findByindexIdAndType(Long indexId, Integer type) {
        QueryWrapper<Attachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Attachment::getIndexId, indexId)
                .eq(Attachment::getType,type);
        List<Attachment> list = list(wrapper);
        List<AttachmentVO> vos = new ArrayList<>();
        if (list !=null && list.size()>0){
            for (Attachment attachment : list) {
                AttachmentVO attachmentVO = BeanUtil.copyProperties(attachment, AttachmentVO.class);
                vos.add(attachmentVO);
            }
        }
        return vos;
    }
}
